package com.dd.census.controller;

import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import com.dd.census.controller.common.BaseController;
import com.dd.census.entity.SysRole;
import com.dd.census.service.impl.SysRoleServiceImpl;
import com.dd.census.tools.SysException;
import com.dd.census.tools.validator.ValidateUtil;

@Controller
@RequestMapping(value = "rest/sysRole")
public class SysRoleController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(SysRoleController.class);

	@Autowired
	private SysRoleServiceImpl sysRoleService;

	@RequestMapping("select")
	public ResponseEntity<Object> select(@RequestBody SysRole sysRole, HttpServletRequest request) {
		try {
			// 基本输入验证
			ValidateUtil.validate(sysRole);

			return this.doSuccess(request, sysRoleService.selectPage(sysRole));

		} catch (SysException e) {
			// 业务异常
			return doFaild(request, e);

		} catch (Exception e) {
			// 系统异常
			logger.error("Internal Server Error", e);
			return doFaild(request, HttpStatus.INTERNAL_SERVER_ERROR);
		}
	}

	@RequestMapping("insert")
	public ResponseEntity<Object> insert(HttpServletRequest request, @RequestBody SysRole sysRole) {

		try {
			// 基本输入验证
			ValidateUtil.validate(sysRole);

			// 插入数据
			sysRole.setCreateUserId(this.getUserId(request));
			sysRoleService.insert(sysRole);

			return this.doSuccess(request);
		} catch (Exception e) {
			// 系统异常
			logger.error("Internal Server Error", e);
			return doFaild(request, HttpStatus.INTERNAL_SERVER_ERROR);
		}
	}

	@RequestMapping("/{id}")
	public ResponseEntity<Object> getById(@PathVariable("id") Integer id, HttpServletRequest request) {
		try {

			return this.doSuccess(request, sysRoleService.selectById(id));
		} catch (Exception e) {
			// 系统异常
			logger.error("Internal Server Error", e);
			return doFaild(request, HttpStatus.INTERNAL_SERVER_ERROR);
		}
	}

	@RequestMapping("update")
	public ResponseEntity<Object> update(HttpServletRequest request, @RequestBody SysRole sysRole) {

		try {
			// 基本输入验证
			ValidateUtil.validate(sysRole);

			// 修改数据
			sysRole.setUpdateUserId(this.getUserId(request));
			sysRoleService.update(sysRole);

			return this.doSuccess(request);
		} catch (Exception e) {
			// 系统异常
			logger.error("Internal Server Error", e);
			return doFaild(request, HttpStatus.INTERNAL_SERVER_ERROR);
		}
	}
}